import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import path from "path";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import ElementPlus from "unplugin-element-plus/vite"; // 不加这个配置，ElMessage出不来

export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());

  const { VITE_PUBLIC_PATH } = env;

  return {
    define: {
      // enable hydration mismatch details in production build
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: "true"
    },
    plugins: [
      vue(),
      vueJsx(),
      // 按需引入，主题色的配置，需要加上 importStyle: 'sass'
      Components({
        resolvers: [ElementPlusResolver({
          importStyle: "sass"
        })]
      }),
      ElementPlus()
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src/")
      }
    },
    base: VITE_PUBLIC_PATH,
    server: {
	  host: '0.0.0.0',
      proxy: {
        "/exam-boot": {
          target: "http://localhost:8080", // 凡是遇到 /api 路径的请求，都映射到 target 属性
          changeOrigin: true
        }
      }
    },
    build: {
      rollupOptions: {
        input: {
          index: path.resolve(__dirname, "index.html")
        },
        output: {

          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: (assetInfo) => {
            if (assetInfo.type === "asset" && /\.(jpe?g|png|gif|svg)$/i.test(assetInfo.name)) {
              return "static/img/[name].[hash][ext]";
            }
            if (assetInfo.type === "asset" && /\.(ttf|woff|woff2|eot)$/i.test(assetInfo.name)) {
              return "static/fonts/[name].[hash][ext]";
            }
            return "static/[ext]/name1-[hash].[ext]";
          },
          manualChunks: {}
        }
      }
    }
  };
});
